local assets =
{
    -- Asset("ANIM", "anim/panda_fisherman_supply_pack.zip"),
    -- Asset( "IMAGE", "images/inventoryimages/panda_fisherman_supply_pack.tex" ),  -- 背包贴图
    -- Asset( "ATLAS", "images/inventoryimages/panda_fisherman_supply_pack.xml" ),
}
local function GetStringsTable()
    return TUNING.NPC_CHAT_TABLE[ type(TUNING.__NPC_LANGUAGE)=="function" and TUNING.__NPC_LANGUAGE() or TUNING.__NPC_LANGUAGE ] ["npc_item_risk_taker_tent"] or {}
end
-----------------------------------------------------------------------------------------------------
local function Find_Location(inst)
    local function find_location(inst)
        local function is_near_doors(pt)
            local ents = TheSim:FindEntities(pt.x, 0, pt.z, 40, nil, nil, {"multiplayer_portal","npc_tag_cave_exit"})
            if #ents > 0 then
                return true
            else
                return false
            end
        end
        -- local find_flag = true
        for i = 1, 100, 1 do
            local pt = inst.npc_base_lib:GetRandomPoint(inst)
            if is_near_doors(pt) == false then
                return pt
            end
        end
        print("Error : npc_item_risk_taker_tent Find_Location nil")
        inst:Remove()
        return false
    end    
    local pt = find_location(inst)
    if pt == false then
        return
    end
    inst.Transform:SetPosition(pt.x,0,pt.z)
    inst:DoTaskInTime(0,function()
        print("info : npc_item_risk_taker_tent  ready")        
    end)
end
-----------------------------------------------------------------------------------------------------
local function World_Event_playeractivated_init(inst)
    inst:ListenForEvent("announce_event",function()
        inst.npc_base_lib:Announce(GetStringsTable().announce,true)        
    end)
    inst.__World_Event_playeractivated_fn = function()
        inst:DoTaskInTime(5,function()
            -- print("info:ms_playerspawn  event",TheWorld:HasTag("cave"),#AllPlayers)
            local announce_flag = false
            for k, v in pairs(AllPlayers) do
                -- print("info player prefab:",v.prefab)
                if v.prefab ~= nil then
                    announce_flag = true
                    break
                end
            end
            if announce_flag == false then
                return
            else
                print("npc info: player join cave and start risk event announce timer")
            end
            inst:DoTaskInTime(60,function()
                if inst:IsNearPlayer(15) == false and math.random(100) < 50 then
                    -- inst.npc_base_lib:Announce(GetStringsTable().announce,true)  
                    inst:PushEvent("announce_event")              
                end
            end)
        end)
        
    end
    
    inst:ListenForEvent("ms_playerspawn", inst.__World_Event_playeractivated_fn, TheWorld)
    inst.Remove__npc_old = inst.Remove
    inst.Remove = function(inst)
        if inst.__World_Event_playeractivated_fn then
            inst:RemoveEventCallback("ms_playerspawn", inst.__World_Event_playeractivated_fn, TheWorld)
        end
        inst:Remove__npc_old()
    end

    if TUNING.NPC_CONFIG.CAVE_RESCUE ~= true then
        inst:DoTaskInTime(0,inst.Remove)
    end
    
end
local function OnLoad_task(inst)
    if inst:IsValid() ~= true then
        return
    end
    inst:DoTaskInTime(0,function()

        local npc_name = inst.npc_everything_data:Get("npc_prefab") or inst.npc_base_lib:GetRandom(TUNING.NPC_CONFIG.NPC_NAME_LIST).."_npc"
        print("info npc event risk npc name :",npc_name)
        inst.npc_everything_data:Set("npc_prefab",npc_name)
        local npc = SpawnPrefab(npc_name)
        local pt = Vector3(inst.Transform:GetWorldPosition())
        npc.Transform:SetPosition(pt.x+1,0,pt.z+1)
        npc:PushEvent("player_link",inst)
        npc:AddDebuff("npc_event_risk_taker", "npc_event_risk_taker")
    end)    
    
end
-----------------------------------------------------------------------------------------------------
local function fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
	inst.entity:AddNetwork()

    inst.entity:AddAnimState()
    inst.AnimState:SetBank("tent_walter")
    inst.AnimState:SetBuild("tent_walter")
    inst.AnimState:PlayAnimation("idle",true)

    MakeInventoryFloatable(inst, nil, 0.05, 0.7)

    inst:AddTag("npc_item_risk_taker_tent")

    inst.entity:SetPristine()
    if not TheWorld.ismastersim then
        return inst
    end
    inst:AddComponent("npc_base_lib")
    inst:AddComponent("npc_everything_data")

    inst:AddComponent("inspectable") --可检查组件
    
    inst:AddComponent("named")
    inst.components.named:SetName(GetStringsTable().name)
    STRINGS.CHARACTERS.GENERIC.DESCRIBE[string.upper("npc_item_risk_taker_tent")] = GetStringsTable().inspect_str  --人物检查的描述

    inst:ListenForEvent("Find_Location",Find_Location) ------ 选择放置位置
    inst:DoTaskInTime(10,OnLoad_task)
    World_Event_playeractivated_init(inst)
    return inst
end

return Prefab("npc_item_risk_taker_tent", fn,assets)